const app = getApp()

// 状态配置
const STATUS_CONFIG = {
  1: { // 正常
    name: '正常',
    icon: 'https://gitee.com/teajoy/image/raw/master/images/正常.png',
    color: '#34d399',
    statusIcon: 'icon-check'
  },
  2: { // 异常
    name: '异常',
    icon: 'https://gitee.com/teajoy/image/raw/master/images/异常.png',
    color: '#f87171',
    statusIcon: 'icon-warning'
  }
}

Page({
  data: {
    record: {
      id: '',
      date: '',
      time: '',
      type: '',
      duration: 0,
      note: '',
      status: '',
      statusIcon: '',
      statusColor: '',
      poopIcon: ''
    }
  },

  onLoad(options) {
    const { id } = options
    if (id) {
      // 从本地存储获取记录详情
      this.loadRecordDetail(id)
    }
  },

  // 加载记录详情
  loadRecordDetail(id) {
    const records = wx.getStorageSync('poopRecords') || []
    const record = records.find(r => r.id === id)
    
    if (record) {
      // 获取类型信息
      const statusInfo = STATUS_CONFIG[record.typeId] || STATUS_CONFIG[1]
      
      // 处理时间，确保包含秒数
      let timeWithSeconds = record.time || '00:00:00' // 添加默认值，防止 undefined
      if (!timeWithSeconds) {
        timeWithSeconds = '00:00:00' // 如果 time 不存在，则使用默认值
      }
      
      // 格式化时长显示
      let durationText = ''
      if (record.durationText) {
        // 如果有预格式化的时长文本，直接使用
        durationText = record.durationText
      } else if (typeof record.duration === 'number' || typeof record.durationSeconds === 'number') {
        // 如果有原始的分钟和秒数数据
        const minutes = record.duration || 0
        const seconds = record.durationSeconds || 0
        durationText = `${minutes}分${seconds}秒`
      } else {
        // 兼容旧数据，将duration作为分钟数
        const minutes = parseInt(record.duration) || 0
        durationText = `${minutes}分0秒`
      }
      
      this.setData({
        record: {
          id: record.id,
          date: record.date,
          time: timeWithSeconds, // 使用处理后的带秒数的时间
          type: statusInfo.name,
          color: statusInfo.color,
          duration: record.duration || 0,
          durationText: durationText,
          note: record.note || '',
          status: statusInfo.name,
          statusIcon: statusInfo.statusIcon,
          statusColor: statusInfo.color,
          poopIcon: statusInfo.icon
        }
      })
    } else {
      this.selectComponent('#customToast').showToast('记录不存在')
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },

  // 编辑记录
  onEdit() {
    const modal = this.selectComponent('#poopNoteModal')
    modal.setData({
      show: true,
      title: '编辑备注',
      placeholder: '请输入备注内容',
      value: this.data.record.note || ''
    })
  },

  // 处理模态框确认事件
  handleModalConfirm(e) {
    const newNote = e.detail.trim()
    // 更新记录
    const records = wx.getStorageSync('poopRecords') || []
    const index = records.findIndex(r => r.id === this.data.record.id)
    
    if (index > -1) {
      // 更新记录
      records[index].note = newNote
      wx.setStorageSync('poopRecords', records)
      
      // 更新页面数据
      this.setData({
        'record.note': newNote
      })
      
      this.selectComponent('#customToast').showToast('保存成功')
    }
  },

  // 处理模态框取消事件
  handleModalClose() {
    // 取消编辑，不做任何操作
  },

  // 删除记录
  onDelete() {
    wx.showModal({
      title: '提示',
      content: '确定要删除这条记录吗？',
      success: (res) => {
        if (res.confirm) {
          // 从本地存储删除记录
          const records = wx.getStorageSync('poopRecords') || []
          const newRecords = records.filter(r => r.id !== this.data.record.id)
          wx.setStorageSync('poopRecords', newRecords)
          
          this.selectComponent('#customToast').showToast('删除成功')
          // 返回上一页并刷新
          const pages = getCurrentPages()
          const prevPage = pages[pages.length - 2]
          if (prevPage) {
            prevPage.onLoad()
          }
          wx.navigateBack()
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { record } = this.data;
    return {
      title: `${record ? record.date : ''}的健康记录`,
      path: `/packageA/pages/detail/detail?id=${record ? record._id : ''}`,
      imageUrl: 'https://gitee.com/teajoy/image/raw/master/images/share-cover.png'
    }
  }
}) 